package com.ruoyi.web.controller.system.assets;

import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.core.domain.Ajax2Result;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.GetSessionValueUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.assets.FzBuildingRoom;
import com.ruoyi.system.domain.assets.FzBuildingUnit;
import com.ruoyi.system.domain.assets.FzBusinessFloor;
import com.ruoyi.system.domain.communiry.FzSCommunity;
import com.ruoyi.system.service.assets.IFzBuildingRoomService;
import com.ruoyi.system.service.assets.IFzBuildingUnitService;
import com.ruoyi.system.service.assets.IFzBusinessFloorService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpSession;

/**
 * 房屋信息Controller
 * 
 * @author ruoyi
 * @date 2020-02-28
 */
@Controller
@RequestMapping("/system/room")
public class FzBuildingRoomController extends BaseController
{
    private String prefix = "system/assets/room";

    @Autowired
    private IFzBusinessFloorService fzBusinessFloorService;
    @Autowired
    private IFzBuildingRoomService fzBuildingRoomService;
    @Autowired
    private IFzBuildingUnitService fzBuildingUnitService;
    @RequiresPermissions("system:room:view")
    @GetMapping()
    public String room(ModelMap map,HttpSession session)
    {
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        FzBusinessFloor fzBusinessFloor=new FzBusinessFloor();
        fzBusinessFloor.setcId(communityId);
        List<FzBusinessFloor> list = fzBusinessFloorService.selectFzBusinessFloorList(fzBusinessFloor);
        map.put("floorSelect",list);
        return prefix + "/room";
    }

    /**
     * 查询房屋信息列表
     */
    @RequiresPermissions("system:room:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpSession session, FzBuildingRoom fzBuildingRoom)
    {
        startPage();
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        fzBuildingRoom.setcId(communityId);
        List<FzBuildingRoom> list = fzBuildingRoomService.selectFzBuildingRoomList(fzBuildingRoom);

        return getDataTable(list);
    }

    /**
     * 导出房屋信息列表
     */
    @RequiresPermissions("system:room:export")
    @Log(title = "房屋信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzBuildingRoom fzBuildingRoom)
    {
        List<FzBuildingRoom> list = fzBuildingRoomService.selectFzBuildingRoomList(fzBuildingRoom);
        ExcelUtil<FzBuildingRoom> util = new ExcelUtil<FzBuildingRoom>(FzBuildingRoom.class);
        return util.exportExcel(list, "room");
    }

    /**
     * 新增房屋信息
     */
    @GetMapping("/add")
    public String add(ModelMap map,HttpSession session)
    {
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        FzBusinessFloor fzBusinessFloor=new FzBusinessFloor();
        fzBusinessFloor.setcId(communityId);
        List<FzBusinessFloor> list = fzBusinessFloorService.selectFzBusinessFloorList(fzBusinessFloor);
        map.put("floorSelectAdd",list);
        return prefix + "/add";
    }

    /**
     * 新增保存房屋信息
     */
    @RequiresPermissions("system:room:add")
    @Log(title = "房屋信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FzBuildingRoom fzBuildingRoom)
    {
        fzBuildingRoom.setRoomId(HeadNum.ROOM_HEAD+ DateUtils.getTimeMillis());
          fzBuildingRoom.setcId(GetSessionValueUtil.getCommunity());
        fzBuildingRoom.setCreateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzBuildingRoomService.insertFzBuildingRoom(fzBuildingRoom));
    }

    /**
     * 修改房屋信息
     */
    @GetMapping("/edit/{roomId}")
    public String edit(@PathVariable("roomId") String roomId, ModelMap mmap)
    {
        FzBuildingRoom fzBuildingRoom = fzBuildingRoomService.selectFzBuildingRoomById(roomId);
        mmap.put("fzBuildingRoom", fzBuildingRoom);
        return prefix + "/edit";
    }

    /**
     * 修改保存房屋信息
     */
    @RequiresPermissions("system:room:edit")
    @Log(title = "房屋信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzBuildingRoom fzBuildingRoom)
    {
        fzBuildingRoom.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzBuildingRoomService.updateFzBuildingRoom(fzBuildingRoom));
    }

    @GetMapping("/untilSelect")
    @ResponseBody
    public String floorSelect(ModelMap map,@Param("floorId") String floorId)
    {
        if (!floorId.equals("")&&!floorId.equals(null)){
        FzBuildingUnit fzBuildingUnit=new FzBuildingUnit();
        fzBuildingUnit.setFloorId(floorId);
        List<FzBuildingUnit> list = fzBuildingUnitService.selectFzBuildingUnitList(fzBuildingUnit);
        return JSONObject.toJSONString(list);}else{
            return null;
        }
    }

    @GetMapping("/roomSelect")
    @ResponseBody
    public String roomSelect(ModelMap map, @Param("unitId") String unitId)
    {
        if (!unitId.equals("")&&!unitId.equals(null)){
            FzBuildingRoom fzBuildingRoom=new FzBuildingRoom();
            fzBuildingRoom.setcId(GetSessionValueUtil.getCommunity());
            fzBuildingRoom.setUnitId(unitId);
            fzBuildingRoom.setState("1");
            List<FzBuildingRoom> list = fzBuildingRoomService.selectFzBuildingRoomList(fzBuildingRoom);
            System.out.println("**************************");
            System.out.println(unitId);
            System.out.println(list);
            return JSONObject.toJSONString(list);}
        else{
            return null;
        }
    }
    /**
     * 删除房屋信息
     */
    @RequiresPermissions("system:room:remove")
    @Log(title = "房屋信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fzBuildingRoomService.deleteFzBuildingRoomByIds(ids));
    }


    //查询房屋信息
    @GetMapping("/roomById/{roomId}")
    @ResponseBody
    public Ajax2Result selectfloorById(@PathVariable("roomId") String roomId)
    {
        FzBuildingRoom fzBuildingRoom = fzBuildingRoomService.selectFzBuildingRoomById(roomId);
        List<FzBuildingRoom>  fzBuildingRooms= Arrays.asList(fzBuildingRoom);
        return Ajax2Result.success(fzBuildingRooms);
    }
}
